001    package net.sf.xdc;
002    
003    /*
004     *  Copyright 2005-2006 Jens Voß.
005     *
006     *  Licensed under the GNU Lesser General Public License (the "License");
007     *  you may not use this file except in compliance with the License.
008     *  You may obtain a copy of the License at
009     *
010     *       http://opensource.org/licenses/lgpl-license.php
011     *
012     *  Unless required by applicable law or agreed to in writing, software
013     *  distributed under the License is distributed on an "AS IS" BASIS,
014     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     *  See the License for the specific language governing permissions and
016     *  limitations under the License.
017     */
018    
019    import java.net.URL;
020    
021    import net.sf.xdc.processing.XdcOptions;
022    import net.sf.xdc.processing.XdcProcessor;
023    import net.sf.xdc.util.ClasspathURLConnection;
024    import net.sf.xdc.util.Logging;
025    import org.apache.commons.cli.CommandLine;
026    import org.apache.commons.cli.CommandLineParser;
027    import org.apache.commons.cli.HelpFormatter;
028    import org.apache.commons.cli.ParseException;
029    import org.apache.log4j.Logger;
030    
031    /**
032     * This class provides the main entry point for the XDC application.
033     *
034     * @author Jens Voß
035     * @since 0.5
036     * @version 0.5
037     */
038    public class Main {
039    
040      private static final Logger LOG = Logging.getLogger();
041    
042      // initialization step, we need to register the factory for "classpath" URL
043      // stream handlers first
044      static {
045        URL.setURLStreamHandlerFactory(new ClasspathURLConnection.StreamHandlerFactory());
046        // another alternative would be to delete the ClasspathURLStreamHandlerFactory,
047        // place the ClasspathURLStreamHandler in a package named "classpath" and
048        // rename it to "Handler". Then, start the application with the VM option
049        //
050        // -Djava.protocol.handler.pkgs=<parent pkg of the "classpath" pkg>
051      }
052    
053      private Main() {
054      }
055    
056      /**
057       * This is the main entry point for the XDC application.
058       *
059       * @param args The command line arguments passed when invoking XDC
060       */
061      public static void main(String[] args) {
062    
063        try {
064    
065          LOG.debug("XDC is starting");
066    
067          CommandLine line = getCommandLine(args);
068          if (line.hasOption("help")) {
069            new HelpFormatter().printHelp("xdc [options] [packagenames] [sourcefiles] [@files]", XdcOptions.getXdcOptions());
070            return;
071          }
072    
073          XdcProcessor processor = new XdcProcessor(line);
074          processor.process();
075    
076          LOG.debug("XDC is finishing");
077    
078        }
079        catch (Throwable e) {
080          LOG.error(e.getMessage(), e);
081        }
082    
083      }
084    
085    
086       static CommandLine getCommandLine(String[] args) throws ParseException {
087        CommandLineParser parser = new XdcParser();
088        return parser.parse(XdcOptions.getXdcOptions(), args);
089      }
090    
091    }